package net.odtel.mailadmin.dao;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.odtel.mailadmin.model.EmailDomain;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class EmailDomainDAOImpl extends HibernateDaoSupport implements
		EmailDomainDAO {

	
	@Transactional(propagation = Propagation.REQUIRED)
	public void add(EmailDomain emailDomain) {
		getHibernateTemplate().save(emailDomain);

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void update(EmailDomain emailDomain) {
		getHibernateTemplate().update(emailDomain);

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void remove(Long emailDomainId) {
		EmailDomain emailDomain = getHibernateTemplate().load(
				EmailDomain.class, emailDomainId);
		if (emailDomain != null)
			getHibernateTemplate().delete(emailDomain);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Map<String, String> getMap() {
		List<EmailDomain> list = getHibernateTemplate().find(
				" from " + EmailDomain.class.getName());
		
		Map<String, String> domains = new LinkedHashMap<String, String>();
		
		for (EmailDomain emailDomain : list) {
			String domain = emailDomain.getDomain();
			domains.put(domain, domain);
		}
		
		return domains;
	}

}
